//
//  StatusOriginalView.swift
//  WeiboSwift
//
//  Created by Apple13 on 16/7/14.
//  Copyright © 2016年 yupeng. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

class StatusOriginalView: UIView {

    //  记录原创微博底部的约束
    var originalViewBottom: Constraint?  //底部约束
    
    //设置ViewModel属性
    var statusViewModel:StatusViewModel? {
        
        didSet {
            //判断url是否为空--即是否有图像
            if let imageUrl = statusViewModel?.status?.user?.profile_image_url{
                
                headImageView.sd_setImageWithURL(NSURL(string:imageUrl), placeholderImage: UIImage(named: "avatar_default_big"))
                
            }
            screenNameLabel.text = statusViewModel?.status?.user?.screen_name
            
            //  设置认证类型等级图片
            verifiedTypeImageView.image = statusViewModel?.verifiedTypeImage ///认证类型④
            
            timeLabel.text = "上世纪"
            sourceLabel.text = "来自 Zippo打火机"
            
            //设置微博内容
            contentLabel.text = statusViewModel?.status?.text
            
            
            //设置来源字符串
            sourceLabel.text = statusViewModel?.sourceContent
            
            //设置会员等级图片
            mbrankImageView.image = statusViewModel?.mbrankImage
            
            //卸载底部约束
            originalViewBottom?.uninstall()
            
            //判断原创微博是否有配图(数据是否大于0)
            if let count = statusViewModel?.status?.pic_urls?.count where count > 0 {
                //  有配图, 设置数据源,更新约束, 显示出来
                pictureView.hidden = false
                pictureView.picUrls = statusViewModel?.status?.pic_urls
                //  更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.originalViewBottom = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
                })
            }else {
                //  没有配图, 不显示,更新约束
                pictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.originalViewBottom = make.bottom.equalTo(contentLabel).offset(HomeCellMargin).constraint
                })
                
            }
            
            
        }
        
    }
    
//MARK: - 懒加载控件
    //头像
    private lazy var headImageView: UIImageView = UIImageView(image:UIImage(named:"avatar_default_big"))
    
    //认证等级图片
    private lazy var verifiedTypeImageView: UIImageView = UIImageView(image:UIImage(named:"avatar_vip"))
    
    //昵称
    private lazy var screenNameLabel:UILabel = UILabel(textColor: UIColor.orangeColor(), fontSize: 12)
    
    //会员等级图标
    private lazy var mbrankImageView:UIImageView = UIImageView(image:UIImage(named:"common_icon_membership"))
    
    //时间
    private lazy var timeLabel:UILabel = UILabel(textColor:UIColor.greenColor(),fontSize: 11)
    
    //来源
    private lazy var sourceLabel:UILabel = UILabel(textColor:UIColor.purpleColor(),fontSize: 11)
    
    //微博内容
    private lazy var  contentLabel:UILabel = {
        let label = UILabel(textColor:UIColor.darkGrayColor(),fontSize:15)
        label.numberOfLines = 0
        return label
    }()
    
    //原创微博配图
    private lazy var pictureView:StatusPictureView = {//设置微博配图①
        let view = StatusPictureView()
        view.backgroundColor = RandomColor()//‼️临时设置
        return view
    }()
    
    
//MARK: - 初始化方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
  //MARK: - 控件自动布局约束设置
    private func setUpUI() {
        backgroundColor = RandomColor()  //增加cell间距③
        
        addSubview(headImageView)
        addSubview(verifiedTypeImageView)
        addSubview(screenNameLabel)
        addSubview(mbrankImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView) //设置微博配图②
        
        headImageView.snp_makeConstraints { (make) in
            make.top.equalTo(self).offset(HomeCellMargin)
            make.leading.equalTo(self).offset(HomeCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        verifiedTypeImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView.snp_trailing)
            make.centerY.equalTo(headImageView.snp_bottom)
        }
        screenNameLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(headImageView.snp_trailing).offset(HomeCellMargin)
            make.top.equalTo(headImageView)
        }
        mbrankImageView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(screenNameLabel.snp_trailing).offset(HomeCellMargin)
            make.top.equalTo(screenNameLabel)
        }
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(headImageView)
            make.leading.equalTo(headImageView.snp_trailing).offset(HomeCellMargin)
        }
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(HomeCellMargin)
            make.bottom.equalTo(timeLabel)
        }
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView.snp_bottom).offset(HomeCellMargin)
            make.leading.equalTo(headImageView)
            make.trailing.equalTo(self).offset(-HomeCellMargin)
            
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(HomeCellMargin)
        }
        
        
        
//‼️ 非常重要的约束 确定cell的高度 用最下面一个控件的高度把cell撑起来
        self.snp_makeConstraints { (make) in  //底部约束

           self.originalViewBottom = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
        }
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}
